import axios from 'axios'
let download = async function (e) {
  console.log(e)
  let response = axios.get(e, {
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    params: {
      'q-sign-algorithm': 'sha1',
      'q-ak': 'AKIDvf9NN5WhonVx94tOfgPqzqPhhQzIJ2Te',
      'q-sign-time': '1679742911;1679746511',
      'q-key-time': '1679742911;1679746511',
      'q-header-list': '',
      'q-url-param-list': '',
      'q-signature': '29d8029c36716f3ca14bd97537df54564b62f9ed'
    },
    responseType: 'blob'
  })
  console.log(response)

  const url = window.URL.createObjectURL(new Blob([response.data]));
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', 'test_export.txt');
  document.body.appendChild(link);
  link.click();
};


let saveFile = async function (data) {
  console.log(data);
  // 将数据转换为Blob对象
  const blob = new Blob([data], { type: "application/octet-stream" });

  // 创建URL对象
  const url = URL.createObjectURL(blob);

  // 创建a标签
  const a = document.createElement("a");
  a.href = url;

  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();

  // 获取当前时间戳
  const timestamp = Date.now();

  // 生成随机数
  const random = Math.floor(Math.random() * 10000);

  // 组合成字符串
  const result = year + '-' + month + '-' + day + '-' + timestamp + '-' + random;
  a.download = `${result}.json`;

  // 添加到DOM树中并模拟点击
  document.body.appendChild(a);
  a.click();

  // 释放URL对象
  URL.revokeObjectURL(url);
}

export default {
  download,
  saveFile
}